package com.j.thetabk.thetas;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by j on 2016/4/13.
 */
public class ThetaSConnectorA extends ThetaSProtocol{

    private String mIpAddress = null;

    public String manufacturer;
    public String model;
    public String serialNumber;
    public String firmwareVersion;
    public String supportUrl;

    public boolean gps;
    public boolean gyro;
    public int uptime;
    public String [] api={ "/osc/info",
            "/osc/state",
            "/osc/checkForUpdates",
            "/osc/commands/execute",
            "/osc/commands/status"
    };
    public int httpPort=80;
    public int httpUpdatesPort=80;

    public ThetaSConnectorA(String cameraIpAddress) {
        mIpAddress = cameraIpAddress;
    }

    protected void ParseInfo(JSONObject response) throws JSONException {
        manufacturer=response.getString("manufacturer");
        model=response.getString("model");
        serialNumber=response.getString("serialNumber");
        firmwareVersion=response.getString("firmwareVersion");
        supportUrl=response.getString("supportUrl");

        gps=response.getBoolean("gps");
        gyro=response.getBoolean("gyro");
        uptime=response.getInt("uptime");

        JSONArray jatemp = response.getJSONArray("api");
        api=new String[jatemp.length()];
        for(int i=0;i<jatemp.length();i++)
            api[i]=jatemp.getString(i);

        httpPort=response.getJSONObject("endpoints").getInt("httpPort");
        httpUpdatesPort=response.getJSONObject("endpoints").getInt("httpUpdatesPort");
    }

    public static String InputStreamToString(InputStream is) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String lineData;
        while ((lineData = br.readLine()) != null) {
            sb.append(lineData);
        }
        br.close();
        return sb.toString();
    }

    public InputStream Transmit(int apiIndex, JSONObject input) throws IOException {
        HttpURLConnection connection;

        URL url = new URL("http://"+mIpAddress+":"+String.valueOf(httpPort)+api[apiIndex]);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        connection.setRequestProperty("Accept", "application/json");
        connection.setDoInput(true);

        if(apiIndex==0) {
            connection.connect();
            return connection.getInputStream();
        }

        connection.setRequestMethod("POST");
        connection.setDoOutput(true);

        if(apiIndex==1)
        {
            connection.connect();
            return connection.getInputStream();
        }

        OutputStream os = connection.getOutputStream();
        os.write(input.toString().getBytes());
        connection.connect();
        os.flush();
        os.close();

        return connection.getInputStream();
    }

    public JSONObject TransmitA(int apiIndex, JSONObject input) throws IOException, JSONException {
        InputStream is = Transmit(apiIndex, input);
        String responseData = InputStreamToString(is);
        return new JSONObject(responseData);
    }

    public void GetInfo() throws JSONException, IOException {
        JSONObject output = TransmitA(0, null);
        ParseInfo(output);
    }

    public double FirmwareVersionNumber()
    {
        return Double.parseDouble(firmwareVersion);
    }

    public String InfoStr()
    {
        return model + "\n" + serialNumber +"\n" + firmwareVersion;
    }

}
